package com.qingfeng.client.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingfeng.base.aop.LogInfo;
import com.qingfeng.client.bean.Attr;
import com.qingfeng.client.dao.AttrMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author ff
 * @Data 2017/10/26 11:53
 */
@Slf4j
@Api(tags = "商品属性接口")
@RestController
@RequestMapping(value = "/api/Attrs")
public class AttrApi extends BaseV1Api{

    @Resource
    private AttrMapper attrDao;


    @LogInfo(action = "获取规格id获取下级规格",param = "'父id：'+#typeId")
    @ApiOperation("获取规格id获取下级规格")
    @GetMapping(value = "/getByType/{typeId}")
    public Object getByType(@PathVariable(value = "typeId")Integer typeId){
        List list =  attrDao.getByType(typeId);
        if (list == null){
         return error(null);
        }else {
            return success(list);
        }
    }

    @LogInfo(action = "获取规格id获取下级规格")
    @ApiOperation("获取规格id获取下级规格")
    @GetMapping(value = "/getAll")
    public Object getByType(){
        List list =  attrDao.selectList(new QueryWrapper<>());
        if (list == null){
            return error(null);
        }else {
            return success(list);
        }
    }


    @ApiOperation("新增商品属性")
    @PostMapping(value = "/add")
    public Object add(@RequestBody Attr attrBean) {
        log.debug("新增商品属性："+attrBean.getName());
        int result = 0;
        if (attrBean != null) {
            result = attrDao.insert(attrBean);
        }
        if (result>0){
            return success(null);
        }else {
            return error(null);
        }
    }

    @ApiOperation("更新商品属性")
    @RequestMapping(value = "/update")
    public Object update(@RequestBody Attr attrBean) {
        log.debug("修改id="+attrBean.getId()+"对应的属性值为："+attrBean.getName());
        int result = 0;
        if (attrBean != null) {
            result = attrDao.updateById(attrBean);
        }
        if (result>0){
            return success(null);
        }else {
            return error(null);
        }
    }

    /**
     * @param id 待删除数据的id ,多个以逗号隔开
     */
    @ApiOperation("删除商品属性")
    @RequestMapping(value = "/delete")
    public Object delete(String id) {
        log.debug("删除某种商品属性，id="+id);
        int result = attrDao.deleteById(id);
        if (result>0){
            return success(null);
        }else {
            return error(null);
        }
    }


}
